/**
 * This class is used to instantiate a Pawn piece that extends the piece class (abstract) with its own 
 * implementations for the movePossible method.
 * 
 * @author Dzulhafizi Abidin
 */
package pieces;
import mainClasses.*;
import java.lang.Math;

public class Pawn extends Piece{
	
	public boolean firstMove = true;			//a boolean value to determine the player's turn
	public boolean moveEnposan = false;
	public Piece enPosanPawn = new NullPiece(10, 10, this.board);
	


	/**
	 * A constructor for the class Pawn. A Pawn piece will be instantiated with the coordinate of (xaxis,yaxis) and
	 * placed on board b, with color set to parameter color. This constructor takes 4 different parameters.
	 * 
	 * @param xaxis - The X-axis coordinate of the pawn
     * @param yaxis - The Y-axis coordinate of the pawn
     * @param b - Board where the pawn is going to be placed
     * @param color - The color of the pawn, black or white
     */
	public Pawn(int xaxis, int yaxis, Board b, String color)
	{
		super(xaxis, yaxis, b, "  Pawn   ", color);     					//calls the constructor in its super class, Piece
		if(color!="White")
		{
			super.type = "BK-->Pawn";     				//If the parameter color does not equals "White", the String type will be set to "BK-->Pawn"
														//So, if String type has no "BK", then it is a white piece
			
		}
	}
	
	/**
     * Accessor: Returns the current x-coordinate value of the piece
     * 
     * @return X-coordinate value of the piece on the board
     */
    public int getoldX()
    {
    	this.firstMove = true;
    	return (int)this.oldx;
    }
    
    
    /**
     * Accessor: Returns the current y-coordinate value of the piece
     * 
     * @return Y-coordinate value of the piece on the board
     */
    public int getoldY()
    {
    	this.firstMove = true;
    	return (int)this.oldy;
    }
    
    
	/**
     * Determines whether the movement to p2 is valid for the pawn. For example, a pawn can only move two squares forward
     * only if it is its first turn and a pawn cannot move sideways. If it's a valid move, then it checks for any interference
     * from any other piece along the path of the movement. A pawn cannot move to a square that is occupied by a piece of its 
     * own side. However, if there's a piece of different color, the enemy's piece can be considered as 'eaten'. If all the validity 
     * tests have been passed, it returns true.
     *
     * @param p2 - Destination of the movement
     * @return A boolean value to determine whether the move is valid or not
     */
	public boolean movePossible(int desX, int desY)
	{
		this.moveEnposan = false;
		if(desX>7||desX<0||desY>7||desY<0)
        {
        	return false;
        }else if(this.board.getPiece(desX, desY).type.equals("         ")||this.isEnemy(desX, desY))
		{
			if((super.y<desY) && (super.x==desX))
			{
				return false;
			}else if((super.y>desY) && (super.x==desX))
			{
				return false;
			}else if((super.x>=desX)&&super.side.equals("White"))
			{
				return false;
			}else if((super.x<=desX)&&super.side.equals("Black"))
			{
				return false;
			}else if(Math.abs(x-desX)==1 && Math.abs(y-desY)==1)
			{
				if(this.isEnemy(desX, desY))
				{
					return true;
				}else if(this.side.equals("White")&&this.x==4&&((y-1)>=0)&&(this.board.getPiece(x, y-1).getSide().equals("Black"))&&(desX==x+1)&&(desY==y-1))
				{
					this.enPosanPawn = this.board.getPiece(x, y-1);
					this.moveEnposan = true;
					return true;
				}else if(this.side.equals("White")&&this.x==4&&((y+1)<8)&&(this.board.getPiece(x, y+1).getSide().equals("Black"))&&(desX==x+1)&&(desY==y+1))
				{
					this.enPosanPawn = this.board.getPiece(x, y+1);
					this.moveEnposan = true;
					return true;
				}else if(this.side.equals("Black")&&this.x==3&&((y-1)>=0)&&(this.board.getPiece(x, y-1).getSide().equals("White"))&&(desX==x-1)&&(desY==y-1))
				{
					this.enPosanPawn = this.board.getPiece(x, y-1);
					this.moveEnposan = true;
					return true;
				}else if(this.side.equals("Black")&&this.x==3&&((y+1)<8)&&(this.board.getPiece(x, y+1).getSide().equals("White"))&&(desX==x-1)&&(desY==y+1))
				{
					this.enPosanPawn = this.board.getPiece(x, y+1);
					this.moveEnposan = true;
					return true;
				}else
				{
					return false;
				}
			}else if((Math.abs(x-desX)+Math.abs(y-desY))==2)
			{
				if(firstMove==true && Math.abs(x-desX)==2)
				{
					if(!(super.board.getPiece(desX, desY) instanceof NullPiece))
					{
						return false;
					}if(this.board.getPiece(this.x+1, this.y).type.equals("         "))
					{
						//firstMove=false;
						return true;
					}else if(this.board.getPiece(this.x-1, this.y).type.equals("         "))
					{
						//firstMove=false;
						return true;
					}else
					{
						return false;
					}
				}else if(firstMove == false && Math.abs(x-desX)==2)
				{
					return false;
				}else if(super.board.getPiece(desX, desY).getSide().equals(super.side))
				{
					return false;
				}else if(super.board.getPiece(desX, desY).getSide().equals("None"))
				{
					return true;
				}else
				{
					return true;
				}
			}else if(Math.abs(super.x-desX)+Math.abs(super.y-desY)==1)
			{
				if(super.board.getPiece(desX, desY).side.equals("None"))
				{
					return true;			
				}else
				{
					return false;
				}
			}
			else if((Math.abs(super.x-desX)+Math.abs(super.y-desY))!=1 || (Math.abs(super.x-desX)+Math.abs(super.y-desY))!=2)
			{
				return false;
			}
			return false;
		}else return false;
	}
	
	
	
    /**
     * Set the firstMove to true
     */
    public void setFirstfalse()
    {
    	this.firstMove = false;
    }
    
    /**
     * Returns true if the current move is the first move of this pawn in the game
     * 
     * @return Boolean set to true if this is the pawn's first move
     */
    public boolean isFirstMove()
    {
		return firstMove;
	}
}
